home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / MDiagArray2.cc < prev    next >
C/C++ Source or Header  |  1996-10-12  |  3KB  |  154 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "MDiagArray2.h"
  32. #include "lo-error.h"
  33.  
  34. #include "MArray-defs.h"
  35.  
  36. // Two dimensional diagonal array with math ops.
  37.  
  38. // Element by element MDiagArray2 by MDiagArray2 ops.
  39.  
  40. template <class T>
  41. MDiagArray2<T>&
  42. operator += (MDiagArray2<T>& a, const MDiagArray2<T>& b)
  43. {
  44.   int r = a.rows ();
  45.   int c = a.cols ();
  46.  
  47.   int b_nr = b.rows ();
  48.   int b_nc = b.cols ();
  49.  
  50.   if (r != b_nr || c != b_nc)
  51.     {
  52.       gripe_nonconformant ("operator +=", r, c, b_nr, b_nc);
  53.       static MDiagArray2<T> foo;
  54.       return foo;
  55.     }
  56.   else
  57.     {
  58.       int l = a.length ();
  59.       DO_VV_OP2 (+=);
  60.     }
  61.   return a;
  62. }
  63.  
  64. template <class T>
  65. MDiagArray2<T>&
  66. operator -= (MDiagArray2<T>& a, const MDiagArray2<T>& b)
  67. {
  68.   int r = a.rows ();
  69.   int c = a.cols ();
  70.  
  71.   int b_nr = b.rows ();
  72.   int b_nc = b.cols ();
  73.  
  74.   if (r != b_nr || c != b_nc)
  75.     {
  76.       gripe_nonconformant ("operator -=", r, c, b_nr, b_nc);
  77.       static MDiagArray2<T> foo;
  78.       return foo;
  79.     }
  80.   else
  81.     {
  82.       int l = a.length ();
  83.       DO_VV_OP2 (-=);
  84.     }
  85.   return a;
  86. }
  87.  
  88. // Element by element MDiagArray2 by scalar ops.
  89.  
  90. #define MARRAY_DAS_OP(OP) \
  91.   template <class T> \
  92.   MDiagArray2<T> \
  93.   operator OP (const MDiagArray2<T>& a, const T& s) \
  94.   { \
  95.     DO_VS_OP (OP); \
  96.     return MDiagArray2<T> (result, a.rows (), a.cols ()); \
  97.   }
  98.  
  99. MARRAY_DAS_OP (*)
  100. MARRAY_DAS_OP (/)
  101.  
  102. // Element by element scalar by MDiagArray2 ops.
  103.  
  104. template <class T>
  105. MDiagArray2<T>
  106. operator * (const T& s, const MDiagArray2<T>& a)
  107. {
  108.   DO_SV_OP (*);
  109.   return MDiagArray2<T> (result, a.rows (), a.cols ());
  110. }
  111.  
  112. // Element by element MDiagArray2 by MDiagArray2 ops.
  113.  
  114. #define MARRAY_DADA_OP(FCN, OP) \
  115.   template <class T> \
  116.   MDiagArray2<T> \
  117.   FCN (const MDiagArray2<T>& a, const MDiagArray2<T>& b) \
  118.   { \
  119.     int r = a.rows (); \
  120.     int c = a.cols (); \
  121.     int b_nr = b.rows (); \
  122.     int b_nc = b.cols (); \
  123.     if (r != b_nr || c != b_nc) \
  124.       { \
  125.         gripe_nonconformant (#FCN, r, c, b_nr, b_nc); \
  126.     return MDiagArray2<T> (); \
  127.       } \
  128.     if (c == 0 || r == 0) \
  129.       return MDiagArray2<T> (); \
  130.     int l = a.length (); \
  131.     DO_VV_OP (OP); \
  132.     return MDiagArray2<T> (result, r, c); \
  133.   }
  134.  
  135. MARRAY_DADA_OP (operator +, +)
  136. MARRAY_DADA_OP (operator -, -)
  137. MARRAY_DADA_OP (product,    *)
  138.  
  139. // Unary MDiagArray2 ops.
  140.  
  141. template <class T>
  142. MDiagArray2<T>
  143. operator - (const MDiagArray2<T>& a)
  144. {
  145.   NEG_V;
  146.   return MDiagArray2<T> (result, a.rows (), a.cols ());
  147. }
  148.  
  149. /*
  150. ;;; Local Variables: ***
  151. ;;; mode: C++ ***
  152. ;;; End: ***
  153. */
  154.